package cn.thr.pethome.pet.service.impl;

import cn.thr.pethome.department.domain.Department;
import cn.thr.pethome.pet.domain.PetType;
import cn.thr.pethome.pet.mapper.PetTypeMapper;
import cn.thr.pethome.pet.service.IPetTypeService;
import cn.thr.pethome.base.service.Impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author thr
 * @since 2023-02-05
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {

    @Autowired
    private PetTypeMapper petTypeMapper;


    @Override
    public List<PetType> PetTree() {
        //第一步查询全部
        List<PetType> petTypes = petTypeMapper.findAll();

        //第三步 儿子找父亲 建立好关系网
        for (PetType petType : petTypes) {
            if(petType.getPid() != null){//找顶级 如过id不等于null就循环找儿子
                for (PetType parent : petTypes) {
                    if(petType.getPid() == parent.getId()){//判断是不是你的父亲
                        List<PetType> children = parent.getChildren();
                        children.add(petType);
                        break; //找到取消循环
                    }
                }
            }
        }
        //第二步找顶级
        List<PetType> firstTree = petTypes.stream()
                .filter(petType -> petType.getPid() == null)//过滤出来父id为null 的话就过滤出来
                .collect(Collectors.toList());//转为list集合
        System.out.println(firstTree);
        return firstTree;
    }
}
